package br.indt.framework.ui.common;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import br.indt.framework.ui.ImageLoader;

/**
 * Main container to host the app user interface.
 * 
 * @author Nokia Institute of Technology
 *
 */
public class Form extends Displayable  {
	private Image bg_color = null;

	public Form(String name) {
		super(name);

		try {
			width = Display.getInstance().getWidth() - (leftMargin + rightMargin);

		} catch (Exception e) {
		//	Utils.logError("Form::constructor - e = " + e);
		}
	}

	public void paint(Graphics g, int x, int y, int maxWidth, int maxHeight) {
		try{
			
		int menuBarH = (menuBar == null) ? 0 : menuBar.getHeight();

		if (!isMenuList()) {
			g.setColor(backgroundColor);
			g.fillRect(x, y, maxWidth, maxHeight);

			if(backgroundImage != null) {
				g.drawImage(backgroundImage, 0, titleBar == null ? 0 : getMaxScrollY(), Graphics.TOP | Graphics.LEFT);
			}

		} else {
			if (menuListShow) {
				try {
					bg_color = ImageLoader.load("/bg_90_opacity.png");
					for (int i = 0; i<= this.getWidth(); i+=10) {
						for (int j = 0; j<= this.getHeight(); j+=10) {
							g.drawImage(bg_color, i, j, Graphics.TOP | Graphics.LEFT);
						}
					}
					menuListShow = false;

				} catch (Exception e) {
					//empty
				}
			}
		}

		int size = items.size();
		Widget widget = null;
		g.setClip(x, 0, maxWidth, scrollAreaHeight+y);
		y = getCurrentY();

		int lastY = (maxHeight - menuBarH) + 3; // +3 is to draw thru the shadow of the menu bar
		x += leftMargin;

		int maxWidthNoMargin = maxWidth - (leftMargin + rightMargin);
		g.setClip(x, 0, maxWidthNoMargin, lastY);

		for(int i = 0; i < size; i++) {
			widget = (Widget) items.elementAt(i);

			if (widget.isSelected()) {
				widget.setSelected(false);
			}

			if (!Display.isTouchAndType() && getEnableElement() == i) {
				try {
					widget.setSelected(true);
					setCurrentItem(i);

				} catch (Exception e) {
					//empty
				}
			}

			if(y + widget.getHeight() > 0) {
				if (widget.isHorizontal()) {
					if (i > 0) {
						Widget widAnt = (Widget) items.elementAt(i - 1);
						widget.setPosY(y - widAnt.getHeight());
						widget.setPosX(x);
						widget.paint(g, x, y - widAnt.getHeight(), maxWidthNoMargin, -1);

					} else {
						widget.setPosY(y);
						widget.setPosX(x);
						widget.paint(g, x, y, maxWidthNoMargin, -1);
					}

				} else {
					widget.setPosY(y);
					widget.setPosX(x);
					widget.paint(g, x, y, maxWidthNoMargin, -1);
				}
			}

			if (!widget.isHorizontal()) {
				y += widget.getHeight();
			}

			if(y > lastY) {
				break;
			}
		}

		if(titleBar != null) {
			g.setClip(0, 0, maxWidth, maxHeight);
			titleBar.paint(g, 0, 0, maxWidth, maxHeight);
		}

		if(menuBar != null) {
		//	if (UIManager.getDeviceType() == UIManager.FULL_TOUCH) {
				menuBarH = 60;
		//	}

			g.setClip(0,  maxHeight - menuBarH, maxWidth, maxHeight);
			menuBar.paint(g, 0, maxHeight - menuBarH, maxWidth, menuBarH);
		}

//		if (!isPopUp()) {
//			g.setClip(0, 0, maxWidth, maxHeight);
//
//			ScrollBar scroll = ScrollBar.getInstance("ScrollBar");
//			scroll.paint(g, maxWidth-4, getCurrentY(), 4, maxHeight-80);
//		}

		} catch (Exception e) {
			//empty
		}
	}

	public int getEnableElement() {
		int i = 0;
		try {
			if (!Display.isTouchAndType() && getCurrentItem() < 0) {
				return i;

			}else {
				for (i = getCurrentItem(); i < items.size(); i++) {
					if (((Widget) items.elementAt(i)).isEnabled()) {
						break;
					}
				}
			}

		} catch (Exception e) {
//			// #ifdef LOGS
//			Utils.log("###>>> ERROR IN Form::getEnableElemnt " + e);
//			// #endif
		}
		return i;
	}

	/*
	 * Return item selected
	 */
	public Object getElementSelected(){
		return items.elementAt(getEnableElement());
	}

	/*
	 * Return item in the position i
	 */
	public Object getItem(int i){
		return items.elementAt(i);
	}
}